home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / bbs / fcfgw40s.zip / FILEAREA.CPP < prev    next >
C/C++ Source or Header  |  1997-04-20  |  36KB  |  1,285 lines

  1. // FileArea.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "FileCFG.h"
  6. #include "FileArea.h"
  7. #include "PropFileLoc.h"
  8. #include "PropFileFlag.h"
  9. #include "PropFileGroup.h"
  10. #include "PropSecurity.h"
  11. #include "SelectGroup.h"
  12. #include "SelectLevel.h"
  13. #include "SelectType.h"
  14. #include "GlobalFile.h"
  15. #include "SortWait.h"
  16. #include "tipdlg.h"
  17. #include <string.h>
  18. #include <stdlib.h>
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. const int _nHugeValues = 32000;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CFileArea dialog
  30.  
  31. char *CFileArea::m_NoText = "╖╖╖<empty definition>╖╖╖";
  32.  
  33. CFileArea::CFileArea(CWnd* pParent /*=NULL*/)
  34.     : CDialog(CFileArea::IDD, pParent)
  35. {
  36.     //{{AFX_DATA_INIT(CFileArea)
  37.     //}}AFX_DATA_INIT
  38. }
  39.  
  40. void CFileArea::DoDataExchange(CDataExchange* pDX)
  41. {
  42.     CDialog::DoDataExchange(pDX);
  43.     //{{AFX_DATA_MAP(CFileArea)
  44.     DDX_Control(pDX, IDC_LIST_FILE_AREAS, m_ListBox);
  45.     //}}AFX_DATA_MAP
  46. }
  47.  
  48.  
  49. BEGIN_MESSAGE_MAP(CFileArea, CDialog)
  50.     //{{AFX_MSG_MAP(CFileArea)
  51.     ON_COMMAND(IDR_MENU_HELP_ABOUT, OnMenuHelpAbout)
  52.     ON_COMMAND(IDR_MENU_EXIT, OnMenuExit)
  53.     ON_REGISTERED_MESSAGE(WM_FIND, OnFindCall)
  54.     ON_COMMAND(IDR_MENU_LOAD, OnMenuLoad)
  55.     ON_COMMAND(IDR_MENU_SELECT_ALL, OnMenuSelectAll)
  56.     ON_COMMAND(IDR_MENU_UNSELECT, OnMenuUnselect)
  57.     ON_COMMAND(IDR_MENU_SWAP, OnMenuSwap)
  58.     ON_COMMAND(IDR_MENU_SAVE, OnMenuSave)
  59.     ON_COMMAND(IDR_MENU_CLEAR, OnMenuClear)
  60.     ON_COMMAND(IDR_MENU_PACK, OnMenuPack)
  61.     ON_COMMAND(IDR_MENU_DELETE, OnMenuDelete)
  62.     ON_COMMAND(IDR_MENU_INSERT, OnMenuInsert)
  63.     ON_COMMAND(IDR_MENU_CLONE, OnMenuClone)
  64.     ON_COMMAND(IDR_MENU_STARDATE, OnMenuStardate)
  65.     ON_LBN_SELCHANGE(IDC_LIST_FILE_AREAS, OnSelchangeListFileAreas)
  66.     ON_COMMAND(IDR_MENU_MOVE_DOWN, OnButtonMoveDown)
  67.     ON_COMMAND(IDR_MENU_MOVE_UP, OnButtonMoveUp)
  68.     ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
  69.     ON_WM_VKEYTOITEM()
  70.     ON_COMMAND(IDR_MENU_POPUP_EDIT, OnMenuEdit)
  71.     ON_WM_CONTEXTMENU()
  72.     ON_COMMAND(IDR_MENU_SELECTBY_GROUP, OnMenuSelectbyGroup)
  73.     ON_COMMAND(IDR_MENU_SELECTBY_LEVEL, OnMenuSelectbyLevel)
  74.     ON_COMMAND(IDR_MENU_TIP_OF_DAY, OnMenuTipOfDay)
  75.     ON_COMMAND(IDR_MENU_SELECTBY_TYPE, OnMenuSelectbyType)
  76.     ON_BN_CLICKED(IDC_BUTTON_GLOBAL, OnButtonGlobal)
  77.     ON_BN_CLICKED(IDC_BUTTON_MOVE_SELECTED, OnButtonMoveSelected)
  78.     ON_BN_CLICKED(IDC_BUTTON_SORT_SELECTED, OnButtonSortSelected)
  79.     ON_COMMAND(IDR_MENU_SORT_ASCENDING, OnMenuSortAscending)
  80.     ON_COMMAND(IDR_MENU_SORT_DESCENDING, OnMenuSortDescending)
  81.     ON_BN_CLICKED(IDC_BUTTON_EXIT, OnMenuExit)
  82.     ON_BN_CLICKED(IDC_BUTTON_SWAP, OnMenuSwap)
  83.     ON_BN_CLICKED(IDC_BUTTON_SAVE, OnMenuSave)
  84.     ON_COMMAND(IDR_MENU_FIND_TEXT, OnButtonFind)
  85.     ON_COMMAND(IDR_MENU_POPUP_CLEAR, OnMenuClear)
  86.     ON_COMMAND(IDR_MENU_POPUP_CLONE, OnMenuClone)
  87.     ON_COMMAND(IDR_MENU_POPUP_DELETE, OnMenuDelete)
  88.     ON_COMMAND(IDR_MENU_POPUP_INSERT, OnMenuInsert)
  89.     ON_LBN_DBLCLK(IDC_LIST_FILE_AREAS, OnMenuEdit)
  90.     ON_BN_CLICKED(IDC_BUTTON_EDIT, OnMenuEdit)
  91.     ON_COMMAND(IDR_MENU_EDIT, OnMenuEdit)
  92.     ON_COMMAND(ID_BUTTON_TB_CLEAR, OnMenuClear)
  93.     ON_COMMAND(ID_BUTTON_TB_DELETE, OnMenuDelete)
  94.     ON_COMMAND(ID_BUTTON_TB_MOVEDOWN, OnButtonMoveDown)
  95.     ON_COMMAND(ID_BUTTON_TB_MOVEUP, OnButtonMoveUp)
  96.     ON_COMMAND(ID_BUTTON_TB_NEW, OnMenuInsert)
  97.     ON_COMMAND(ID_BUTTON_TB_CLONE, OnMenuClone)
  98.     ON_COMMAND(IDR_MENU_GLOBAL, OnButtonGlobal)
  99.     ON_COMMAND(IDR_MENU_MOVE, OnButtonMoveSelected)
  100.     //}}AFX_MSG_MAP
  101. END_MESSAGE_MAP()
  102.  
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CFileArea message handlers
  106.  
  107. void CFileArea::OnMenuHelpAbout() 
  108. {
  109.     CAboutDlg dlg;
  110.     dlg.DoModal();
  111. }
  112.  
  113. void CFileArea::OnMenuExit() 
  114. {
  115.     if( shouldSave ){
  116.         CString msg;
  117.  
  118.         msg.LoadString(IDS_SHOULD_SAVE);
  119.         switch(MessageBox(msg, m_Title,MB_ICONQUESTION|MB_YESNOCANCEL))
  120.         {
  121.             case IDCANCEL: return;    // don't quit yet
  122.             case IDYES   : OnMenuSave(); // fall-through
  123.             case IDNO    : break;
  124.         }
  125.     }
  126.     OnOK();
  127. }
  128.  
  129. BOOL CFileArea::OnInitDialog() 
  130. {
  131.     CDialog::OnInitDialog();
  132.  
  133.     // this should always work (checked by program already)
  134.     strcpy(m_SysPath, getenv("PROBOARD"));
  135.     m_FindDlgInited = FALSE;
  136.  
  137.     // load the common caption for the message boxes
  138.     m_Title.LoadString(IDS_TITLE);
  139.  
  140.     // load the toolbar icons
  141.     CRect rect;
  142.     GetDlgItem(IDC_STATIC_TOOLBAR_FRAME)->GetWindowRect(&rect);
  143.     ScreenToClient(&rect);
  144.     rect.DeflateRect(1, 1);
  145.     m_ToolBar.Create(WS_VISIBLE|WS_CHILD|CCS_NODIVIDER|CCS_NORESIZE,
  146.                      rect, this, IDB_BITMAP_TOOLBAR);
  147.     LoadData();
  148.  
  149.     return FALSE;    // because the list box is focused
  150. }
  151.  
  152.  
  153. BOOL CFileArea::LoadData()
  154. {
  155.     CFile         file;
  156.     TFileArea     area;
  157.     CProgressDlg *bar;
  158.     UINT          bytes;
  159.     short         nAreas;
  160.     char          path[_MAX_PATH];    
  161.  
  162.     sprintf(path, "%s\\FILECFG.PRO", m_SysPath);
  163.     if(!file.Open(path,CFile::modeRead|CFile::shareDenyWrite))
  164.         return FALSE;
  165.  
  166.     nAreas = short(file.GetLength() / sizeof(TFileArea));
  167.     
  168.     // conservative estimate: 40 characters/area
  169.     if(LB_ERRSPACE == m_ListBox.InitStorage(nAreas, UINT(nAreas)*40)){
  170.         file.Close();
  171.         return FALSE;
  172.     }
  173.  
  174.     bar = new CProgressDlg;
  175.     bar->Create(this);
  176.     bar->SetRange(0, nAreas);
  177.     bar->SetStep(1);
  178.  
  179.     bytes = file.Read(&area, sizeof(area));
  180.     while( bytes == sizeof(TFileArea) ){
  181.         if( '\0' == area.name[0] ){
  182.             bar->SetStatus("<empty area>");
  183.             m_ListBox.AddString(m_NoText);
  184.         }
  185.         else{
  186.             CString name = area.name;
  187.             name.OemToAnsi();
  188.             bar->SetStatus(name);
  189.             m_ListBox.AddString(name);
  190.         }
  191.         bar->StepIt();
  192.         bytes = file.Read(&area, sizeof(area));
  193.     }
  194.     file.Close();
  195.  
  196.     // set the internal area numbers
  197.     SetNumberData();
  198.     shouldSave = FALSE;
  199.     GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  200.     m_ListBox.SetSel(0, TRUE);
  201.     delete bar;
  202.     return TRUE;
  203. }
  204.  
  205. // set the internal numbers to match the list
  206. void CFileArea::SetNumberData()
  207. {
  208.     for(int i = 0; i < m_ListBox.GetCount(); ++i)
  209.         m_ListBox.SetItemData(i, DWORD(i));
  210. }
  211.  
  212. // load the data from the file (dispose of current)
  213. void CFileArea::OnMenuLoad() 
  214. {
  215.     if( shouldSave ){
  216.         CString msg;
  217.         msg.LoadString(IDS_PROMPT_SAVE);
  218.         switch(MessageBox(msg,m_Title,MB_ICONQUESTION|MB_YESNOCANCEL)){
  219.             case IDCANCEL: return;
  220.             case IDYES   : OnMenuSave();
  221.             case IDNO    : break;
  222.         }
  223.     }
  224.  
  225.     m_ListBox.ResetContent();
  226.     if( !LoadData() ){
  227.         CString msg;
  228.         msg.LoadString(IDS_ERROR_LOAD);
  229.         MessageBox(msg,m_Title, MB_ICONERROR|MB_OK);
  230.     }
  231. }
  232.  
  233. // select all items in the list
  234. void CFileArea::OnMenuSelectAll() 
  235. {
  236.     m_ListBox.SelItemRange(TRUE, 0, m_ListBox.GetCount()-1);
  237.     OnSelchangeListFileAreas();
  238. }
  239.  
  240. // deselect all items in the list
  241. void CFileArea::OnMenuUnselect() 
  242. {
  243.     m_ListBox.SelItemRange(FALSE, 0, m_ListBox.GetCount()-1);
  244.     OnSelchangeListFileAreas();
  245. }
  246.  
  247. // swap two areas
  248. void CFileArea::OnMenuSwap() 
  249. {
  250.     int   select[2];
  251.     DWORD data;
  252.     char  buf[2][80];
  253.     
  254.     if( 2 != m_ListBox.GetSelCount() ){
  255.         CString msg;
  256.  
  257.         msg.LoadString(IDS_SWAP_PROBLEM);
  258.         MessageBox(msg,m_Title, MB_ICONINFORMATION);
  259.     }
  260.     else{
  261.         m_ListBox.GetSelItems(2, LPINT(select));
  262.         m_ListBox.GetText(select[0], buf[0]);
  263.         m_ListBox.GetText(select[1], buf[1]);
  264.         data = m_ListBox.GetItemData(select[1]);
  265.         m_ListBox.DeleteString(select[1]);
  266.         m_ListBox.InsertString(select[1], buf[0]);
  267.         m_ListBox.SetItemData(select[1], m_ListBox.GetItemData(select[0]));
  268.         m_ListBox.DeleteString(select[0]);
  269.         m_ListBox.InsertString(select[0], buf[1]);
  270.         m_ListBox.SetItemData(select[0], data);
  271.         m_ListBox.SetCaretIndex(select[1], FALSE);
  272.         m_ListBox.SetSel(select[0], TRUE);
  273.         m_ListBox.SetSel(select[1], TRUE);
  274.         shouldSave = TRUE;
  275.     }
  276. }
  277.  
  278.  
  279. // dump the list to the file
  280. void CFileArea::OnMenuSave() 
  281. {
  282.     char          in[_MAX_PATH], out[_MAX_PATH];
  283.     char         *statText = "<empty area>";
  284.     CFile         fin, fout;
  285.     DWORD         maxCount;
  286.     TFileArea     area;
  287.     CProgressDlg *bar;
  288.     CString       msg;
  289.  
  290.     sprintf(in, "%s\\FILECFG.PRO", m_SysPath);
  291.     sprintf(out, "%s\\FILECFG.BIN", m_SysPath);
  292.     if(!fin.Open(in, CFile::modeRead|CFile::shareDenyWrite)){
  293.         msg.LoadString(IDS_ERROR_OPEN);
  294.         MessageBox(msg, m_Title, MB_ICONERROR|MB_OK);
  295.         return;
  296.     }
  297.     if(!fout.Open(out, CFile::modeCreate|CFile::modeWrite)){
  298.         fin.Close();
  299.         msg.LoadString(IDS_ERROR_CREATE);
  300.         MessageBox(msg, m_Title, MB_ICONERROR|MB_OK);
  301.         return;
  302.     }
  303.  
  304.     maxCount = fin.GetLength() / sizeof(TFileArea);
  305.     
  306.     bar = new CProgressDlg;
  307.     bar->Create(this);
  308.     bar->SetRange(0, m_ListBox.GetCount());
  309.     bar->SetStep(1);
  310.  
  311.     for(int i = 0; i < m_ListBox.GetCount(); ++i){
  312.         DWORD item = m_ListBox.GetItemData(i);
  313.         // see if we have a cloned area
  314.         if( item > _nHugeValues ) item -= _nHugeValues;
  315.  
  316.         // currently not in the file, dump it
  317.         if( item >= maxCount ){
  318.             memset(&area, 0, sizeof(area));
  319.             bar->SetStatus(statText);
  320.         }
  321.         else{
  322.             fin.Seek(item * sizeof(TFileArea), CFile::begin);
  323.             fin.Read(&area, sizeof(area));
  324.             if( '\0' == area.name[0] )
  325.                 bar->SetStatus(statText);
  326.             else{
  327.                 CString name = area.name;
  328.                 name.OemToAnsi();
  329.                 bar->SetStatus(name);
  330.             }
  331.         }
  332.         fout.Write(&area, sizeof(area));
  333.         bar->StepIt();
  334.     }
  335.  
  336.     fin.Close();
  337.     fout.Close();
  338.  
  339.     CFile::Remove(in);
  340.     CFile::Rename(out, in);
  341.     SetNumberData();
  342.     delete bar;
  343.     shouldSave = FALSE;
  344. }
  345.  
  346. // clears the current area(s): set string to no
  347. // text and the ID number to high (as if it's
  348. // not in the original file)
  349. void CFileArea::OnMenuClear() 
  350. {
  351.     int  count = m_ListBox.GetSelCount();
  352.     int *buf = new int [count];
  353.  
  354.     if( !buf ){
  355.         return;
  356.     }
  357.  
  358.     m_ListBox.GetSelItems(count, buf);
  359.     for(int i = 0; i < count; ++i){
  360.         m_ListBox.DeleteString(buf[i]);
  361.         m_ListBox.InsertString(buf[i], m_NoText);
  362.         m_ListBox.SetItemData(buf[i], _nHugeValues);
  363.     }
  364.     m_ListBox.SetSel(buf[0], TRUE);
  365.     GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  366.     shouldSave = TRUE;
  367.     delete buf;
  368. }
  369.  
  370.  
  371. // removes all empty areas from the list
  372. void CFileArea::OnMenuPack() 
  373. {
  374.     int  count = 0;
  375.     char buf[80];
  376.  
  377.     for(int i = 0; i < m_ListBox.GetCount(); ++i){
  378.         m_ListBox.GetText(i, buf);
  379.         if( !stricmp(m_NoText, buf) ){
  380.             m_ListBox.DeleteString(i);
  381.             i--;
  382.             count++;
  383.         }
  384.     }
  385.     OnMenuUnselect();
  386.     m_ListBox.SetCaretIndex(0, FALSE);
  387.     m_ListBox.SetSel(0, TRUE);
  388.  
  389.     if( count ){
  390.         sprintf(buf, "Removed %d areas from the list.", count);
  391.         shouldSave = TRUE;
  392.     }
  393.     else
  394.         strcpy(buf, "No areas were removed from the list.");
  395.     
  396.     MessageBox(buf, m_Title, MB_ICONINFORMATION|MB_OK);
  397. }
  398.  
  399. // delete current (or selected) items
  400. void CFileArea::OnMenuDelete() 
  401. {
  402.     CString  msg;
  403.     int      n = m_ListBox.GetSelCount();
  404.     int     *buf;
  405.  
  406.     if( 1 < n ){
  407.         msg.LoadString(IDS_WARN_DELETE);
  408.         if(IDYES != MessageBox(msg, m_Title, MB_ICONQUESTION|MB_YESNO))
  409.             return;
  410.     }
  411.  
  412.     buf = new int [n];
  413.     if( buf ){
  414.         m_ListBox.GetSelItems(n, buf);
  415.         for( int i = 0; i < n; ++i ){
  416.             m_ListBox.DeleteString(buf[i] - i);
  417.         }
  418.         if( buf[0] >= m_ListBox.GetCount() )
  419.             m_ListBox.SetSel(m_ListBox.GetCount()-1, TRUE);
  420.         else
  421.             m_ListBox.SetSel(buf[0], TRUE);
  422.         GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  423.         shouldSave = TRUE;
  424.         delete buf;
  425.     }
  426. }
  427.  
  428. // inserts an area at the current position
  429. void CFileArea::OnMenuInsert() 
  430. {
  431.     int index = 0;
  432.  
  433.     if( 10000 == m_ListBox.GetCount() ){
  434.         CString msg;
  435.         msg.LoadString(IDS_MAX_AREAS);
  436.         MessageBox(msg, m_Title, MB_ICONEXCLAMATION|MB_OK);
  437.         return;
  438.     }
  439.     
  440.     if( m_ListBox.GetCount() ) m_ListBox.GetSelItems(1, &index);
  441.     m_ListBox.InsertString(index, m_NoText);
  442.     m_ListBox.SetItemData(index, _nHugeValues);
  443.     m_ListBox.SetSel(index, TRUE);
  444.     m_ListBox.SetSel(index+1, FALSE);
  445.     GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  446.     shouldSave = TRUE;
  447. }
  448.  
  449. // clone the currently selected area
  450. void CFileArea::OnMenuClone() 
  451. {
  452.     char  buf[80];
  453.     int   index;
  454.  
  455.     if( 1 != m_ListBox.GetSelCount() ){
  456.         CString msg;
  457.         msg.LoadString(IDS_ERROR_CLONE);
  458.         MessageBox(msg,m_Title,MB_OK|MB_ICONSTOP);
  459.         return;
  460.     }
  461.     m_ListBox.GetSelItems(1, &index);
  462.     m_ListBox.GetText(index, buf);
  463.     m_ListBox.InsertString(index+1, buf);
  464.     m_ListBox.SetItemData(index+1, _nHugeValues + m_ListBox.GetItemData(index));
  465.     m_ListBox.SetCaretIndex(index+1, FALSE);
  466.     m_ListBox.SetSel(index, FALSE);
  467.     m_ListBox.SetSel(index+1, TRUE);
  468.     GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  469.     shouldSave = TRUE;
  470. }
  471.  
  472. // display current stardate
  473. void CFileArea::OnMenuStardate() 
  474. {
  475.     char       s[80];
  476.     CTime      t = CTime::GetCurrentTime();
  477.     struct tm *tp = t.GetLocalTm();
  478.     UINT       perCent;
  479.  
  480.     perCent = (UINT(tp->tm_hour)*3600
  481.               + tp->tm_min*60 + tp->tm_sec);
  482.     perCent = perCent * 100 / 86400;
  483.  
  484.     sprintf(s, "Today's stardate is %02d%02d%03d.%02d",
  485.         ((tp->tm_year + 1900) / 100) + 1,
  486.         (tp->tm_year + 1900) % 100,
  487.         tp->tm_yday, (int)perCent);
  488.  
  489.     MessageBox(s, m_Title, MB_OK);
  490. }
  491.  
  492. // called whenever the selection changes
  493. // enable or disable the buttons here
  494. void CFileArea::OnSelchangeListFileAreas() 
  495. {
  496.     CMenu *menu = GetMenu();
  497.  
  498.     // adjust the last selected item which will be
  499.     // used as an anchor for the group move/sort
  500.     // this will be the index where the items will be
  501.     // moved to
  502.     if( 0 == m_ListBox.GetSelCount() ){
  503.         m_AnchorIndex = -1;
  504.     }
  505.     else{
  506.         m_AnchorIndex = m_ListBox.GetCaretIndex();
  507.     }
  508.  
  509.     // more than one item is selected
  510.     if( 1 < m_ListBox.GetSelCount() ){
  511.         if( 2 == m_ListBox.GetSelCount() ){
  512.             GetDlgItem(IDC_BUTTON_SWAP)->EnableWindow(TRUE);
  513.             menu->EnableMenuItem(IDR_MENU_SWAP, MF_ENABLED);
  514.         }
  515.         else{
  516.             GetDlgItem(IDC_BUTTON_SWAP)->EnableWindow(FALSE);
  517.             menu->EnableMenuItem(IDR_MENU_SWAP, MF_GRAYED);
  518.         }
  519.         GetDlgItem(IDC_BUTTON_SORT_SELECTED)->EnableWindow(TRUE);
  520.         GetDlgItem(IDC_BUTTON_MOVE_SELECTED)->EnableWindow(TRUE);
  521.         GetDlgItem(IDC_BUTTON_GLOBAL)->EnableWindow(TRUE);
  522.         GetDlgItem(IDC_BUTTON_EDIT)->EnableWindow(FALSE);
  523.         menu->EnableMenuItem(IDR_MENU_MOVE_UP, MF_GRAYED);
  524.         menu->EnableMenuItem(IDR_MENU_MOVE_DOWN, MF_GRAYED);
  525.         menu->EnableMenuItem(IDR_MENU_MOVE, MF_ENABLED);
  526.         menu->EnableMenuItem(IDR_MENU_SORT_ASCENDING, MF_ENABLED);
  527.         menu->EnableMenuItem(IDR_MENU_SORT_DESCENDING, MF_ENABLED);
  528.         menu->EnableMenuItem(IDR_MENU_GLOBAL, MF_ENABLED);
  529.         menu->EnableMenuItem(IDR_MENU_EDIT, MF_GRAYED);
  530.         menu->EnableMenuItem(IDR_MENU_CLONE, MF_GRAYED);
  531.     }
  532.     else{ // one or none items are selected
  533.         GetDlgItem(IDC_BUTTON_SORT_SELECTED)->EnableWindow(FALSE);
  534.         GetDlgItem(IDC_BUTTON_MOVE_SELECTED)->EnableWindow(FALSE);
  535.         GetDlgItem(IDC_BUTTON_GLOBAL)->EnableWindow(FALSE);
  536.         GetDlgItem(IDC_BUTTON_SWAP)->EnableWindow(FALSE);
  537.         GetDlgItem(IDC_BUTTON_EDIT)->EnableWindow(TRUE);
  538.         menu->EnableMenuItem(IDR_MENU_MOVE, MF_GRAYED);
  539.         menu->EnableMenuItem(IDR_MENU_SORT_ASCENDING, MF_GRAYED);
  540.         menu->EnableMenuItem(IDR_MENU_SORT_DESCENDING, MF_GRAYED);
  541.         menu->EnableMenuItem(IDR_MENU_GLOBAL, MF_GRAYED);
  542.         menu->EnableMenuItem(IDR_MENU_MOVE_UP, MF_ENABLED);
  543.         menu->EnableMenuItem(IDR_MENU_MOVE_DOWN, MF_ENABLED);
  544.         menu->EnableMenuItem(IDR_MENU_SWAP, MF_GRAYED);
  545.         menu->EnableMenuItem(IDR_MENU_EDIT, MF_ENABLED);
  546.         menu->EnableMenuItem(IDR_MENU_CLONE, MF_ENABLED);
  547.  
  548.         // adjust the find item
  549.         m_ListBox.GetSelItems(1, &m_FindIndex);
  550.     }
  551. }
  552.  
  553. // move currently selected area down
  554. void CFileArea::OnButtonMoveDown() 
  555. {
  556.     int index;
  557.  
  558.     if( 1 == m_ListBox.GetSelCount() ){
  559.         m_ListBox.GetSelItems(1, &index);
  560.         if( index < m_ListBox.GetCount() - 1 ){
  561.             char  buf[80];
  562.             DWORD data;
  563.             
  564.             data = m_ListBox.GetItemData(index);
  565.             m_ListBox.GetText(index, buf);
  566.             m_ListBox.DeleteString(index);
  567.             m_ListBox.InsertString(index+1, buf);
  568.             m_ListBox.SetItemData(index+1, data);
  569.             m_ListBox.SetCaretIndex(index+1, FALSE);
  570.             m_ListBox.SetSel(index+1, TRUE);
  571.             GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  572.             shouldSave = TRUE;
  573.         }
  574.     }
  575. }
  576.  
  577. // moves one area up in the list
  578. void CFileArea::OnButtonMoveUp() 
  579. {
  580.     int index;
  581.  
  582.     if( 1 == m_ListBox.GetSelCount() ){
  583.         m_ListBox.GetSelItems(1, &index);
  584.         if( 0 != index ){
  585.             char  buf[80];
  586.             DWORD data;
  587.  
  588.             m_ListBox.GetText(index, buf);
  589.             data = m_ListBox.GetItemData(index);
  590.             m_ListBox.DeleteString(index);
  591.             m_ListBox.InsertString(index-1, buf);
  592.             m_ListBox.SetItemData(index-1, data);
  593.             m_ListBox.SetCaretIndex(index-1, FALSE);
  594.             m_ListBox.SetSel(index-1, TRUE);
  595.             GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  596.             shouldSave = TRUE;
  597.         }
  598.     }
  599. }
  600.  
  601. // initialize the find dialog
  602. void CFileArea::OnButtonFind() 
  603. {
  604.     
  605.     if( FALSE == m_FindDlgInited ){
  606.         CFindReplaceDialog *p = new CFindReplaceDialog;
  607.         p->Create(TRUE, "", NULL, FR_DOWN|FR_HIDEWHOLEWORD, this);
  608.         m_FindDlgInited = TRUE;
  609.     }
  610.     else MessageBeep(MB_ICONHAND);
  611. }
  612.  
  613. // the callback function
  614. LONG CFileArea::OnFindCall(WPARAM, LPARAM lParam)
  615. {
  616.     CFindReplaceDialog *p;
  617.     static char lastRequest[80] = "";
  618.  
  619.     p = CFindReplaceDialog::GetNotifier(lParam);
  620.     if( !p ) return 0;
  621.  
  622.     if( p->IsTerminating() ){
  623.         p->DestroyWindow();
  624.         m_FindDlgInited = FALSE;
  625.         return 1;
  626.     }
  627.  
  628.     // ok, we have a find criteria, use it
  629.     int     i, nInc, anchor;
  630.     char    buf[80];
  631.     CString criteria = p->GetFindString(), match;
  632.  
  633.     // this is a new search
  634.     if( 0 != criteria.Compare(lastRequest) ){
  635.         strcpy(lastRequest, LPCTSTR(criteria));
  636.         if( p->SearchDown() ) m_FindIndex = -1;
  637.         else m_FindIndex = m_ListBox.GetCount();
  638.     }
  639.  
  640.     // set the anchors
  641.     if( p->SearchDown() ){
  642.         if( m_ListBox.GetCount() <= m_FindIndex ) m_FindIndex = -1;
  643.         i = m_FindIndex + 1;
  644.         nInc = 1;
  645.         anchor = m_ListBox.GetCount();
  646.     }
  647.     else{
  648.         if( -1 == m_FindIndex ) m_FindIndex = m_ListBox.GetCount();
  649.         i = m_FindIndex - 1;
  650.         nInc = -1;
  651.         anchor = -1;
  652.     }
  653.  
  654.     // don't want case-sensitive, make it uppercase
  655.     if( !p->MatchCase() ) criteria.MakeUpper();
  656.  
  657.     // scan through the whole list now
  658.     for( ; i != anchor; i += nInc ){
  659.  
  660.         if( p->SearchDown() ){
  661.             if( i == m_ListBox.GetCount() ) i = 0;
  662.         }
  663.         else{
  664.             if( -1 == i ) i = m_ListBox.GetCount() - 1;
  665.         }
  666.  
  667.         m_ListBox.GetText(i, buf);
  668.         match = buf;
  669.         if( !p->MatchCase() ) match.MakeUpper();
  670.  
  671.         if( -1 != match.Find(LPCTSTR(criteria)) ){
  672.             m_FindIndex = i;
  673.             m_ListBox.SetCaretIndex(i, FALSE);
  674.             m_ListBox.SetSel(i, !m_ListBox.GetSel(i));
  675.             GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  676.             return 1;
  677.         }
  678.     }
  679.  
  680.     //if we got here, there was no match
  681.     criteria = p->GetFindString();    // restore the case (hehehe)
  682.     char msg[255];
  683.  
  684.     sprintf(msg, "Could not find the string '%s'.", LPCTSTR(criteria));
  685.     MessageBox(msg, m_Title, MB_OK|MB_ICONEXCLAMATION);
  686.     return 0;
  687. }
  688.  
  689. // process keys from the list box
  690. int CFileArea::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex) 
  691. {
  692.     switch( nKey ){
  693.         case VK_BACK  : OnMenuClear();  return -2;
  694.         case VK_INSERT: OnMenuInsert(); return -2;
  695.         case VK_DELETE: OnMenuDelete(); return -2;
  696.         case VK_RETURN: OnMenuEdit();   return -2;
  697.         default: break;
  698.     }
  699.     return CDialog::OnVKeyToItem(nKey, pListBox, nIndex);
  700. }
  701.  
  702. // override to trap the Escape button
  703. void CFileArea::OnCancel()
  704. {
  705.     OnMenuExit();
  706. }
  707.  
  708. // edit the current selection in the list, if any
  709. void CFileArea::OnMenuEdit() 
  710. {
  711.     int        index;
  712.     DWORD      areaNum;
  713.     TFileArea  area;
  714.     CFile      file;
  715.     char       path[_MAX_PATH];
  716.     CString    msg;
  717.     
  718.     if( 1 != m_ListBox.GetSelCount() ){
  719.         msg.LoadString(IDS_ERROR_NO_SELECT);
  720.         MessageBox(msg, m_Title, MB_OK|MB_ICONEXCLAMATION);
  721.         return;
  722.     }
  723.  
  724.     m_ListBox.GetSelItems(1, &index);
  725.     areaNum = m_ListBox.GetItemData(index);
  726.     // is this area has been added, save to disk first
  727.     if( _nHugeValues <= areaNum ){
  728.         msg.LoadString(IDS_SAVE_FIRST);
  729.         if( IDOK != MessageBox(msg, m_Title,MB_YESNO|MB_ICONQUESTION) )
  730.             return;
  731.         OnMenuSave();
  732.         areaNum = m_ListBox.GetItemData(index);
  733.     }
  734.     // now load the area definition from the file
  735.     sprintf(path, "%s\\FILECFG.PRO", m_SysPath);
  736.     if(!file.Open(path,CFile::modeRead|CFile::shareDenyWrite)){
  737.         msg.LoadString(IDS_ERROR_LOAD);
  738.         MessageBox(msg, m_Title,MB_OK|MB_ICONSTOP);
  739.         return;
  740.     }
  741.     file.Seek(areaNum * sizeof(TFileArea), CFile::begin);
  742.     file.Read(&area, sizeof(area));
  743.     file.Close();
  744.  
  745.     // now create the property sheet and display it
  746.     CPropFileLoc   filePage;
  747.     CPropFileFlag  flagPage;
  748.     CPropFileGroup groupPage;
  749.     CPropSecurity  securityPage;
  750.  
  751.     // remove the help button from the pages
  752.     filePage.m_psp.dwFlags &= ~PSP_HASHELP;
  753.     flagPage.m_psp.dwFlags &= ~PSP_HASHELP;
  754.     groupPage.m_psp.dwFlags &= ~PSP_HASHELP;
  755.     securityPage.m_psp.dwFlags &= ~PSP_HASHELP;
  756.  
  757.     // move data to the pages
  758.     filePage.m_AreaName = area.name;
  759.     filePage.m_AreaName.OemToAnsi();
  760.     filePage.m_FileListPath = area.listpath;
  761.     filePage.m_FileLocation = area.filepath;
  762.     filePage.m_DateFormat = area.dateFormat;
  763.     flagPage.m_CDROM = area.copyLocal;
  764.     flagPage.m_Free = area.free;
  765.     flagPage.m_Tops = BOOL(!area.notops);
  766.     flagPage.m_MaxFiles = area.maxfiles;
  767.     flagPage.m_MaxBytes = area.maxkb;
  768.     securityPage.m_Age = area.minAge;
  769.     securityPage.m_Level = area.level;
  770.     // must implement security flags here
  771.     if( 0 != area.groups[0] ){
  772.         groupPage.m_CheckGroup1 = TRUE;
  773.         groupPage.m_Value1 = area.groups[0] - 1;
  774.     }
  775.     if( 0 != area.groups[1] ){
  776.         groupPage.m_CheckGroup2 = TRUE;
  777.         groupPage.m_Value2 = area.groups[1] - 1;
  778.     }
  779.     if( 0 != area.groups[2] ){
  780.         groupPage.m_CheckGroup3 = TRUE;
  781.         groupPage.m_Value3 = area.groups[2] - 1;
  782.     }
  783.     if( 0 != area.groups[3] ){
  784.         groupPage.m_CheckGroup4 = TRUE;
  785.         groupPage.m_Value4 = area.groups[3] - 1;
  786.     }
  787.     groupPage.m_AllGroups = area.allGroups;
  788.     
  789.     // add the pages to the property sheet
  790.     CPropertySheet propSheet(area.name);
  791.     propSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
  792.     propSheet.m_psh.dwFlags &= ~PSH_HASHELP;
  793.     propSheet.AddPage(&filePage);
  794.     propSheet.AddPage(&securityPage);
  795.     propSheet.AddPage(&flagPage);
  796.     propSheet.AddPage(&groupPage);
  797.  
  798.     if( IDOK == propSheet.DoModal() ){
  799.         filePage.m_AreaName.AnsiToOem();
  800.         strcpy(area.name, filePage.m_AreaName);
  801.         strcpy(area.listpath, filePage.m_FileListPath);
  802.         strcpy(area.filepath, filePage.m_FileLocation);
  803.         area.dateFormat = filePage.m_DateFormat;
  804.         area.copyLocal = flagPage.m_CDROM;
  805.         area.free = flagPage.m_Free;
  806.         area.notops = !flagPage.m_Tops;
  807.         area.maxfiles = flagPage.m_MaxFiles;
  808.         area.maxkb = flagPage.m_MaxBytes;
  809.         area.minAge = securityPage.m_Age;
  810.         area.level = securityPage.m_Level;
  811.         area.allGroups = groupPage.m_AllGroups;
  812.  
  813.         if( TRUE == groupPage.m_CheckGroup1 )
  814.             area.groups[0] = (uchar)groupPage.m_Value1 + 1;
  815.         else area.groups[0] = 0;
  816.         if( TRUE == groupPage.m_CheckGroup2 )
  817.             area.groups[1] = (uchar)groupPage.m_Value2 + 1;
  818.         else area.groups[1] = 0;
  819.         if( TRUE == groupPage.m_CheckGroup3 )
  820.             area.groups[2] = (uchar)groupPage.m_Value3 + 1;
  821.         else area.groups[2] = 0;
  822.         if( TRUE == groupPage.m_CheckGroup4 )
  823.             area.groups[3] = (uchar)groupPage.m_Value4 + 1;
  824.         else area.groups[3] = 0;
  825.  
  826.         if(!file.Open(path,CFile::typeBinary|CFile::modeWrite)){
  827.             msg.LoadString(IDS_ERROR_SAVE);
  828.             MessageBox(msg, m_Title, MB_OK|MB_ICONSTOP);
  829.             return;
  830.         }
  831.         file.Seek(areaNum * sizeof(TFileArea), CFile::begin);
  832.         file.Write(&area, sizeof(area));
  833.         file.Close();
  834.         m_ListBox.DeleteString(areaNum);
  835.         if( '\0' == area.name[0] ) m_ListBox.InsertString(areaNum, m_NoText);
  836.         else m_ListBox.InsertString(areaNum, area.name);
  837.         m_ListBox.SetItemData(areaNum, areaNum);
  838.         m_ListBox.SetCaretIndex(areaNum, FALSE);
  839.         m_ListBox.SetSel(areaNum, TRUE);
  840.         GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  841.     }
  842. }
  843.  
  844. void CFileArea::OnContextMenu(CWnd*, CPoint point) 
  845. {
  846.     CPoint local = point;
  847.     ScreenToClient(&local);
  848.     if( GetDlgItem(IDC_LIST_FILE_AREAS) == ChildWindowFromPoint(local) ){
  849.         UINT  nEnable = MF_GRAYED;
  850.         CMenu menu;
  851.  
  852.         menu.LoadMenu(IDR_MENU_LIST_POPUP);
  853.         CMenu *popup = menu.GetSubMenu(0);
  854.         ASSERT(NULL != popup);
  855.  
  856.         if( 1 == m_ListBox.GetSelCount() ) nEnable = MF_ENABLED;
  857.         popup->EnableMenuItem(IDR_MENU_POPUP_CLONE, nEnable);
  858.         popup->EnableMenuItem(IDR_MENU_POPUP_EDIT, nEnable);
  859.         popup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this);
  860.     }
  861. }
  862.  
  863. void CFileArea::OnMenuSelectbyGroup() 
  864. {
  865.     CString msg;
  866.     if( shouldSave ){
  867.         msg.LoadString(IDS_SAVE_FIRST);
  868.         if( IDOK != MessageBox(msg,m_Title,MB_YESNO|MB_ICONQUESTION) )
  869.             return;
  870.         OnMenuSave();
  871.     }
  872.  
  873.     CSelectGroup *dlg = new CSelectGroup;
  874.     if( dlg ){
  875.         if( IDOK == dlg->DoModal() ){
  876.             if( -1 == dlg->m_GroupNum && !dlg->m_SelectAll ){
  877.                 msg.LoadString(IDS_INVALID_GROUP);
  878.                 MessageBox(msg, m_Title,MB_OK|MB_ICONEXCLAMATION);
  879.             }
  880.             else{
  881.                 CFile file;
  882.                 char  path[_MAX_PATH];
  883.                 
  884.                 sprintf(path, "%s\\FILECFG.PRO", getenv("PROBOARD"));
  885.                 if( !file.Open(path,CFile::modeRead|CFile::typeBinary|CFile::shareDenyWrite) ){
  886.                     msg.LoadString(IDS_ERROR_LOAD);
  887.                     MessageBox(msg, m_Title,MB_OK|MB_ICONSTOP);
  888.                 }
  889.                 else{
  890.                     TFileArea area;
  891.  
  892.                     OnMenuUnselect();
  893.                     UINT nBytes = file.Read(&area, sizeof(area));
  894.  
  895.                     for(int i = 0; nBytes == sizeof(area); ++i){
  896.                         if( dlg->m_SelectAll && area.allGroups )
  897.                             m_ListBox.SetSel(i, TRUE);
  898.                         else if( dlg->m_GroupNum != -1 ) {
  899.                             for( int j = 0; j < 4; ++j ){
  900.                                 if( area.groups[j] == dlg->m_GroupNum + 1 ){
  901.                                     m_ListBox.SetSel(i, TRUE);
  902.                                     break;
  903.                                 }
  904.                             }
  905.                         }
  906.                         nBytes = file.Read(&area, sizeof(area));
  907.                     }
  908.                     file.Close();
  909.                 }
  910.             }
  911.         }
  912.         delete dlg;
  913.     }
  914.     // see if we need to adjust the buttons and commands
  915.     OnSelchangeListFileAreas();
  916.     m_ListBox.SetCaretIndex(m_ListBox.GetCaretIndex(), 0);
  917. }
  918.  
  919. void CFileArea::OnMenuSelectbyLevel() 
  920. {
  921.     CString msg;
  922.  
  923.     if( shouldSave ){
  924.         msg.LoadString(IDS_SAVE_FIRST);
  925.         if( IDOK != MessageBox(msg,m_Title,MB_YESNO|MB_ICONQUESTION) )
  926.             return;
  927.         OnMenuSave();
  928.     }
  929.  
  930.     CSelectLevel *dlg = new CSelectLevel;
  931.     if( dlg ){
  932.         if( IDOK == dlg->DoModal() ){
  933.             CFile     file;
  934.             TFileArea area;
  935.             char      path[_MAX_PATH];
  936.  
  937.             sprintf(path, "%s\\FILECFG.PRO", getenv("PROBOARD"));
  938.             if( !file.Open(path, CFile::modeRead|CFile::typeBinary|CFile::shareDenyWrite) ){
  939.                 msg.LoadString(IDS_ERROR_LOAD);
  940.                 MessageBox(msg,m_Title,MB_OK|MB_ICONSTOP);
  941.  
  942.             }
  943.             else{
  944.                 OnMenuUnselect();
  945.                 UINT nBytes = file.Read(&area, sizeof(area));
  946.                 for(int i = 0; nBytes == sizeof(area); ++i){
  947.                     if( area.level == dlg->m_Level ){
  948.                         m_ListBox.SetSel(i, TRUE);
  949.                     }
  950.                     else if( area.level < dlg->m_Level ){
  951.                         if( 1 == dlg->m_MatchLevel ) m_ListBox.SetSel(i, TRUE);
  952.                     }
  953.                     else{
  954.                         if( 2 == dlg->m_MatchLevel ) m_ListBox.SetSel(i, TRUE);
  955.                     }
  956.                     nBytes = file.Read(&area, sizeof(area));
  957.                 }
  958.                 file.Close();
  959.             }
  960.         }
  961.         delete dlg;
  962.     }
  963.     // see if we need to adjust the buttons and commands
  964.     OnSelchangeListFileAreas();
  965.     m_ListBox.SetCaretIndex(m_ListBox.GetCaretIndex(), 0);
  966. }
  967.  
  968. void CFileArea::OnMenuTipOfDay() 
  969. {
  970.     CTipDlg dlg;
  971.     dlg.DoModal();
  972. }
  973.  
  974. void CFileArea::OnMenuSelectbyType() 
  975. {
  976.     CString msg;
  977.  
  978.     if( shouldSave ){
  979.         msg.LoadString(IDS_SAVE_FIRST);
  980.         if( IDOK != MessageBox(msg,m_Title,MB_YESNO|MB_ICONQUESTION) )
  981.             return;
  982.         OnMenuSave();
  983.     }
  984.  
  985.     CSelectType *dlg = new CSelectType;
  986.     if( dlg ){
  987.         if( IDOK == dlg->DoModal() ){
  988.             CFile     file;
  989.             TFileArea area;
  990.             char      path[_MAX_PATH];
  991.  
  992.             sprintf(path, "%s\\FILECFG.PRO", getenv("PROBOARD"));
  993.             if( !file.Open(path, CFile::modeRead|CFile::typeBinary|CFile::shareDenyWrite) ){
  994.                 msg.LoadString(IDS_ERROR_LOAD);
  995.                 MessageBox(msg,m_Title,MB_OK|MB_ICONSTOP);
  996.  
  997.             }
  998.             else{
  999.                 OnMenuUnselect();
  1000.                 UINT nBytes = file.Read(&area, sizeof(area));
  1001.                 for(int i = 0; nBytes == sizeof(area); ++i){
  1002.                     BOOL mustSelect = FALSE;
  1003.                     
  1004.                     // the flags have three states: on/off/don't care
  1005.                     // we also have two boolean operations: or/and
  1006.  
  1007.                     // we want CD-ROM areas, and this is one
  1008.                     // or non-CDROM areas and this is one
  1009.                     if( (0 == dlg->m_CDROM && area.copyLocal) ||
  1010.                         (1 == dlg->m_CDROM && !area.copyLocal) )
  1011.                     {
  1012.                         mustSelect = TRUE;
  1013.                     }
  1014.                     
  1015.                     // we want free areas and this is one
  1016.                     // or non-free areas (and this is one)
  1017.                     if( (0 == dlg->m_Free && area.free) ||
  1018.                         (1 == dlg->m_Free && !area.free) )
  1019.                     {
  1020.                         // AND mode, no previous match and no ignore on first
  1021.                         if( 1 == dlg->m_Or && !mustSelect && 2 != dlg->m_CDROM)
  1022.                         {
  1023.                             ;// skip
  1024.                         }
  1025.                         else mustSelect = TRUE;
  1026.                     }
  1027.                     
  1028.                     // we want areas included in tops and this is one
  1029.                     // or excluded from the tops and this is one
  1030.                     if( (0 == dlg->m_Tops && !area.notops) ||
  1031.                         (1 == dlg->m_Tops && area.notops) )
  1032.                     {
  1033.                         // AND mode, no previous match and no ignore on either of first 2
  1034.                         if( 1 == dlg->m_Or && !mustSelect &&
  1035.                             2 != dlg->m_CDROM && 2 != dlg->m_Free)
  1036.                         {
  1037.                             ; // skip
  1038.                         }
  1039.                         else mustSelect = TRUE;
  1040.                     }
  1041.                     
  1042.                     if( mustSelect ) m_ListBox.SetSel(i, TRUE);
  1043.                     nBytes = file.Read(&area, sizeof(area));
  1044.                 }
  1045.                 file.Close();
  1046.             }
  1047.         }
  1048.         delete dlg;
  1049.     }
  1050.     // see if we need to adjust the buttons and commands
  1051.     OnSelchangeListFileAreas();
  1052.     m_ListBox.SetCaretIndex(m_ListBox.GetCaretIndex(), 0);
  1053. }
  1054.  
  1055. // global changes (must have areas selected)
  1056. void CFileArea::OnButtonGlobal() 
  1057. {
  1058.     CString     msg;
  1059.     CGlobalFile dlg;
  1060.  
  1061.     if( shouldSave ){
  1062.         msg.LoadString(IDS_SAVE_FIRST);
  1063.         if( IDOK != MessageBox(msg, m_Title, MB_YESNO|MB_ICONQUESTION) )
  1064.             return;
  1065.         OnMenuSave();
  1066.     }
  1067.  
  1068.     if( IDOK == dlg.DoModal() ){
  1069.         CFile file;
  1070.         char  path[_MAX_PATH];
  1071.  
  1072.         sprintf(path, "%s\\FILECFG.PRO", getenv("PROBOARD"));
  1073.         if( !file.Open(path, CFile::modeReadWrite|CFile::typeBinary|CFile::shareDenyWrite) ){
  1074.             msg.LoadString(IDS_ERROR_LOAD);
  1075.             MessageBox(msg, m_Title, MB_OK|MB_ICONSTOP);
  1076.             return;
  1077.         }
  1078.  
  1079.         CProgressDlg progress;
  1080.         progress.Create(this);
  1081.         progress.SetRange(0, m_ListBox.GetCount());
  1082.         progress.SetStep(1);
  1083.  
  1084.         for( int i = 0; i < m_ListBox.GetCount(); ++i ){
  1085.             CString name;
  1086.             m_ListBox.GetText(i, name);
  1087.             progress.SetStatus(name);
  1088.             progress.StepIt();
  1089.  
  1090.             if( m_ListBox.GetSel(i) ){
  1091.                 TFileArea area;
  1092.                 CString   s;
  1093.                 int       nPos;
  1094.  
  1095.                 file.Seek((long)i * sizeof(area), CFile::begin);
  1096.                 file.Read(&area, sizeof(TFileArea));
  1097.  
  1098.  
  1099.                 // check the replacement texts
  1100.                 if( dlg.m_CheckListEdit ){
  1101.                     s = area.listpath;
  1102.                     nPos = s.Find(dlg.m_ListReplace);
  1103.                     if( -1 != nPos ){
  1104.                         CString slp = s.Left(nPos);
  1105.                         slp += dlg.m_ListWith;
  1106.                         slp += s.Right(s.GetLength() - nPos - dlg.m_ListReplace.GetLength());
  1107.                         strncpy(area.listpath, slp, sizeof(area.listpath));
  1108.                         area.listpath[sizeof(area.listpath)-1] = '\0';
  1109.                     }
  1110.                 }
  1111.                 if( dlg.m_CheckFileLocation ){
  1112.                     s = area.filepath;
  1113.                     nPos = s.Find(dlg.m_LocationReplace);
  1114.                     if( -1 != nPos ){
  1115.                         CString slp = s.Left(nPos);
  1116.                         slp += dlg.m_LocationWith;
  1117.                         slp += s.Right(s.GetLength() - nPos - dlg.m_LocationReplace.GetLength());
  1118.                         strncpy(area.filepath, slp, sizeof(area.filepath));
  1119.                         area.filepath[sizeof(area.filepath)-1] = '\0';
  1120.                     }
  1121.                 }
  1122.  
  1123.                 // check the access control restrictions
  1124.                 if( dlg.m_CheckMinAge ) area.minAge = dlg.m_MinAge;
  1125.                 if( dlg.m_CheckMaxFiles ) area.maxfiles = dlg.m_MaxFiles;
  1126.                 if( dlg.m_CheckMaxBytes ) area.maxkb = dlg.m_MaxBytes;
  1127.                 if( dlg.m_CheckLevel ) area.level = dlg.m_Level;
  1128.  
  1129.                 // check the flag settings
  1130.                 if( 2 != dlg.m_AllGroups ) area.allGroups = dlg.m_AllGroups;
  1131.                 if( 2 != dlg.m_CDROM ) area.copyLocal = dlg.m_CDROM;
  1132.                 if( 2 != dlg.m_Tops ) area.notops = !dlg.m_Tops;
  1133.                 if( 2 != dlg.m_Free ) area.free = dlg.m_Free;
  1134.  
  1135.                 // check for new group numbers
  1136.                 if( dlg.m_CheckGroup1 ) area.groups[0] = (uchar)dlg.m_Group1 + 1;
  1137.                 if( dlg.m_CheckGroup2 ) area.groups[1] = (uchar)dlg.m_Group2 + 1;
  1138.                 if( dlg.m_CheckGroup3 ) area.groups[2] = (uchar)dlg.m_Group3 + 1;
  1139.                 if( dlg.m_CheckGroup4 ) area.groups[3] = (uchar)dlg.m_Group4 + 1;
  1140.  
  1141.                 file.Seek((long)i * sizeof(area), CFile::begin);
  1142.                 file.Write(&area, sizeof(TFileArea));
  1143.             }
  1144.         }
  1145.         file.Close();
  1146.     }
  1147. }
  1148.  
  1149. void CFileArea::OnButtonMoveSelected() 
  1150. {
  1151.     int index = m_AnchorIndex;
  1152.  
  1153.     if( -1 == m_AnchorIndex ){
  1154.         CString msg, title;
  1155.  
  1156.         msg.LoadString(IDS_MOVE_SELECTED);
  1157.         title.LoadString(IDS_TITLE);
  1158.         MessageBox(msg, title, MB_OK|MB_ICONEXCLAMATION);
  1159.         return;
  1160.     }
  1161.  
  1162.     for( int i = 0; i < m_ListBox.GetCount(); ++i ){
  1163.         if( m_ListBox.GetSel(i) ){    // the item is selected
  1164.             // move it to the index position
  1165.             CString text;
  1166.             DWORD data;
  1167.             
  1168.             data = m_ListBox.GetItemData(i);
  1169.             m_ListBox.GetText(i, text);
  1170.             m_ListBox.DeleteString(i);
  1171.             if( i < index ){
  1172.                 index--;    // adjust insertion point
  1173.                 m_AnchorIndex--;    // adjust focus
  1174.             }
  1175.             m_ListBox.InsertString(index, text);
  1176.             m_ListBox.SetItemData(index, data);
  1177.             --i;        // will get ++ to the same item
  1178.             ++index;    // index moves to next position
  1179.         }
  1180.     }
  1181.     // indicate changes have been made, focus, etc.
  1182.     shouldSave = TRUE;
  1183.     m_ListBox.SetCaretIndex(m_AnchorIndex, 0);
  1184.     m_ListBox.SetSel(m_AnchorIndex, TRUE);
  1185.     m_AnchorIndex = -1;
  1186.     GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  1187.     OnSelchangeListFileAreas();
  1188. }
  1189.  
  1190. // pop up a menu to prompt for the direction and
  1191. // excute the necessary routine
  1192. void CFileArea::OnButtonSortSelected() 
  1193. {
  1194.     CMenu menu;
  1195.     CRect rect;
  1196.  
  1197.     GetDlgItem(IDC_BUTTON_SORT_SELECTED)->GetWindowRect(&rect);
  1198.     menu.LoadMenu(IDR_MENU_SORT_POPUP);
  1199.     CMenu *popup = menu.GetSubMenu(0);
  1200.     ASSERT(NULL != popup);
  1201.  
  1202.     popup->TrackPopupMenu(TPM_RIGHTALIGN|TPM_RIGHTBUTTON,
  1203.             rect.TopLeft().x, rect.BottomRight().y, this);
  1204. }
  1205.  
  1206. void CFileArea::MoveSorted(BOOL ascending)
  1207. {
  1208.     CSortWait prog;
  1209.     int       index = m_AnchorIndex;
  1210.  
  1211.     if( -1 == m_AnchorIndex ){
  1212.         CString msg, title;
  1213.  
  1214.         msg.LoadString(IDS_MOVE_SELECTED);
  1215.         title.LoadString(IDS_TITLE);
  1216.         MessageBox(msg, title, MB_OK|MB_ICONEXCLAMATION);
  1217.         return;
  1218.     }
  1219.  
  1220.     prog.Create(this);
  1221.     prog.m_Progress.SetRange(0, m_ListBox.GetCount() - 1);
  1222.  
  1223.     for( int i = 0; i < m_ListBox.GetCount(); ++i ){
  1224.  
  1225.         prog.StepIt();
  1226.  
  1227.         if( m_ListBox.GetSel(i) ){    // the item is selected
  1228.             // move it to the index position, 
  1229.             CString text;
  1230.             DWORD data;
  1231.             
  1232.             data = m_ListBox.GetItemData(i);
  1233.             m_ListBox.GetText(i, text);
  1234.             m_ListBox.DeleteString(i);
  1235.             if( i < index ){
  1236.                 index--;    // adjust insertion point
  1237.                 m_AnchorIndex--;    // adjust focus
  1238.             }
  1239.  
  1240.             // find a place to insert the string between
  1241.             // the starting position m_AnchorIndex and the
  1242.             // last point of insertion, index. This depends
  1243.             // on the value of 'ascending'
  1244.             for( int j = m_AnchorIndex; j <= index; ++j ){
  1245.                 CString curText;
  1246.                 m_ListBox.GetText(j, curText);
  1247.                 int nStat = text.CompareNoCase(curText);
  1248.  
  1249.                 if( (j == index) ||
  1250.                     (1 == nStat && !ascending) ||
  1251.                     (-1 == nStat && ascending) ||
  1252.                     (0 == nStat) )
  1253.                 {
  1254.                     m_ListBox.InsertString(j, text);
  1255.                     m_ListBox.SetItemData(j, data);
  1256.                     break;
  1257.                     
  1258.                 }
  1259.             }
  1260.             --i;        // will get ++ to the same item
  1261.             ++index;    // index moves to next position
  1262.         }
  1263.     }
  1264.     // indicate changes have been made, focus, etc.
  1265.     shouldSave = TRUE;
  1266.     m_ListBox.SetCaretIndex(m_AnchorIndex, 0);
  1267.     m_ListBox.SetSel(m_AnchorIndex, TRUE);
  1268.     m_AnchorIndex = -1;
  1269.     GotoDlgCtrl(GetDlgItem(IDC_LIST_FILE_AREAS));
  1270.     OnSelchangeListFileAreas();
  1271. }
  1272.  
  1273. // the simple handlers for the menu commands
  1274. // the both call the sorting routine with direction
  1275. void CFileArea::OnMenuSortAscending() 
  1276. {
  1277.     MoveSorted(TRUE);
  1278. }
  1279.  
  1280. void CFileArea::OnMenuSortDescending() 
  1281. {
  1282.     MoveSorted(FALSE);
  1283. }
  1284.  
  1285.